﻿using System;
using System.Configuration;
using System.Web.Mvc;
using System.Web.Security;
using BisinessLogic.Implementations;
using BusinessLogic.Implementations;
using BusinessLogic.Interfaces;
using Domain;
using Ninject;

namespace Web
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel ninjectKernel;

        public NinjectControllerFactory()
        {
            ninjectKernel= new StandardKernel();
            AddBindings();
        }
        // Извлекаем экземляр контролера для заданного контекста  и типа контролера

        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            return controllerType==null ? null :(IController) ninjectKernel.Get(controllerType);
        }

        // Определяем все привязки

        private void AddBindings()
        {
            ninjectKernel.Bind<IUsersRepository>().To<EFUserRepository>();
            ninjectKernel.Bind<IRoleRepository>().To<EFRoleRepository>();
            ninjectKernel.Bind<IProducerRepository>().To<EFProducerRepository>();
            ninjectKernel.Bind<IPrepackageRepository>().To<EFPrepackageRepository>(); 
            ninjectKernel.Bind<ICategoryRepository>().To<EFCategoryRepository>();
            ninjectKernel.Bind<IProduceInPackageRepository>().To<EFProduceInPackageRepository>();
            ninjectKernel.Bind<IProduceRepository>().To<EFProduceRepository>();
            ninjectKernel.Bind<ICartRepository>().To<EFCartRepository>();

            ninjectKernel.Bind<IOrderDetailRepository>().To<EFOrderDetailRepository>();
            ninjectKernel.Bind<IOrderRepository>().To<EFOrderRepository>();
            ninjectKernel.Bind<IOrderStatusRepository>().To<EFOrderStatusRepository>();

            ninjectKernel.Bind<EFDbContext>()
                .ToSelf()
                .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["PrimaryConnectionString"].ConnectionString);

            ninjectKernel.Inject(Membership.Provider);
            ninjectKernel.Inject(Roles.Provider);
        }

    }
}